﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace YJKArchMethodLibrary.Utils
{
	public class Node
	{
		public XYZ Position
		{
			get
			{
				return this.m_ptPosition;
			}
		}

		public List<Pole> PoleList
		{
			get
			{
				return this.m_PoleList;
			}
		}

		public void AddPole(Pole pole)
		{
			this.m_PoleList.Add(pole);
		}

		public void RemoveEdge(int index)
		{
			if (index > 0 && index < this.m_PoleList.Count)
			{
				this.m_PoleList.RemoveAt(index);
				this.AnticlockwiseSortElements();
			}
		}

		public void AnticlockwiseSortElements()
		{
			this.m_PoleList.Sort(new AngleComparer(this.m_ptPosition, 0.0));
		}

		public void ClockwiseSortElements()
		{
			this.m_PoleList.Sort(new AngleComparer(this.m_ptPosition, 0.0));
			this.m_PoleList.Reverse();
		}

		public int FindLinkPoleWithOtherNode(XYZ ptOtherNode, Node.SearchType searchType)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.m_PoleList.Count; i++)
			{
				XYZ xyz = this.m_PoleList[i].CurEndPoint(this.m_ptPosition);
				if (xyz != null && Geometry.IsEqual(ptOtherNode, xyz, 0.032))
				{
					list.Add(i);
				}
			}
			if (list.Count < 1)
			{
				return -1;
			}
			if (searchType == Node.SearchType._Clockwise)
			{
				return list[0];
			}
			return list[list.Count - 1];
		}

		public Node(Node rhs)
		{
			this.m_ptPosition = rhs.m_ptPosition;
			this.m_PoleList = rhs.m_PoleList;
		}

		public Node(XYZ ptPosition, List<Pole> poleList)
		{
			this.m_ptPosition = ptPosition;
			this.m_PoleList = poleList;
		}

		private XYZ m_ptPosition;

		private List<Pole> m_PoleList = new List<Pole>();

		public enum SearchType
		{
			_Anticlockwise,
			_Clockwise
		}
	}
}
